package com.easy.oss.controller;

import com.easy.framework.common.model.R;
import com.easy.framework.database.page.Pagination;
import com.easy.module.log.annotation.Log;
import com.easy.module.log.enums.Operation;
import com.easy.oss.model.query.ItemQuery;
import com.easy.oss.model.vo.BucketVO;
import com.easy.oss.model.vo.ItemVO;
import com.easy.oss.service.MinioService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Minio对象存储端点
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/oss")
public class MinioController {

    private final MinioService minioService;

    /**
     * 获取桶列表
     */

    @PreAuthorize("@perm.hasPermission('oss$list')")
    @GetMapping("/list")
    public R<List<BucketVO>> getBucketList() {
        return R.success(minioService.getBucketList());
    }


    /**
     * 获取桶下文件列表
     */

    @PreAuthorize("@perm.hasPermission('oss$list')")
    @GetMapping("/{bucketName}/list")
    public R<Pagination<ItemVO>> getItemList(@PathVariable("bucketName") String bucketName, ItemQuery query) {
        return R.success(minioService.getItemList(bucketName, query));
    }


    /**
     * 删除桶下文件
     */

    @Log(title = "删除桶下文件", type = Operation.DELETE)
    @PreAuthorize("@perm.hasPermission('oss$remove')")
    @DeleteMapping("/{bucketName}/remove")
    public R getItemList(@PathVariable("bucketName") String bucketName, @RequestBody List<String> fileNameList) {
        return R.success(minioService.remove(bucketName, fileNameList));
    }

}
